package com.ruoyi.common.utils;
import cn.hutool.extra.qrcode.QrCodeUtil;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

@Slf4j
public class QRCodeUtils {
    public static String generateQRCodeBase64(String text, int width, int height) {
        BufferedImage generate = QrCodeUtil.generate(text,
                width, height);
        ByteArrayOutputStream res = new ByteArrayOutputStream();
        try {
            ImageIO.write(generate, "png", res);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        byte[] bytes = res.toByteArray();

        return "data:image/png;base64,"+Base64.getEncoder().encodeToString(bytes);
    }

}
